package com.yuandian.business.monitor.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.AlarmParams;
import com.yuandian.api.business_monitor.vo.BusiAdvancedAlarmVo;
import com.yuandian.business.monitor.mapper.BusiAdvancedAlarmMapper;
import com.yuandian.business.monitor.service.IBusiAdvancedAlarmService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.poi.ExcelTitleUtils;
import com.yuandian.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 自定义告警 Service
 * </p>
 *
 * @author fucx
 * @since 2020-11-10
 */
@Slf4j
@Service
public class BusiAdvancedAlarmServiceImpl implements IBusiAdvancedAlarmService {

	@Autowired
	private BusiAdvancedAlarmMapper busiAdvancedAlarmMapper;

	/**
     * 查询自定义告警数据
     * @param params 告警参数
     * @return 通用返回对象
     */
	@Override
	public PageDataInfo getAdvancedAlarm(AlarmParams params) {
		Page<BusiAdvancedAlarmVo> page = new Page<>(params.getPageNum(), params.getPageSize());

		//目前高级告警没有按天分表
		String tableName = "ads_busi_custom_alarm_m";
		params.setTableName(tableName);

		//去掉前后空格
		if(StringUtils.isNotBlank(params.getAlarmContext())) {
			params.setAlarmContext(params.getAlarmContext().trim());
		}

		List<BusiAdvancedAlarmVo> list = busiAdvancedAlarmMapper.queryAdvanceAlarm(page,params);
		return new PageDataInfo(page, list);
	}

	/**
	 * 自定义告警数据-下载
	 *
	 * @param params 告警参数
	 */
	@Override
	public void exportExcel(AlarmParams params,HttpServletResponse response) {
		//目前高级告警没有按天分表
		String tableName = "ads_busi_custom_alarm_m";
		params.setTableName(tableName);

		//去掉前后空格
		if(StringUtils.isNotBlank(params.getAlarmContext())) {
			params.setAlarmContext(params.getAlarmContext().trim());
		}

		List<BusiAdvancedAlarmVo> list = busiAdvancedAlarmMapper.queryAdvanceAlarm(null,params);
		//添加序号
		int index = 1;
		for (BusiAdvancedAlarmVo busiAdvancedAlarmVo : list) {
			busiAdvancedAlarmVo.setId(index++);
		}
		//设置大标题
		String title = "自定义告警列表";
		List<String> titleParams = new ArrayList<>();
		titleParams.add(title);
//		titleParams.add(searchOptionService.querySystemName(params.getSystemId()));
		titleParams.add(params.getSystemName()); // TODO 前端再原来基础上新增 'systemName' 请求参数
		String formTitleName = ExcelTitleUtils.formTitleName(titleParams, params.getStartTime(), params.getEndTime());
		//需要展示的字段
		List<String> displayFields = Arrays.asList("序号","告警级别","告警时间","指标","告警对象","业务名称","应用节点","告警信息");
		List<List<String>> headList = new ArrayList<>();
		displayFields.forEach(s -> {
			List<String> childHeadList = new ArrayList<>();
			childHeadList.add(formTitleName);
			childHeadList.add(s);
			headList.add(childHeadList);
		});
		try {
			ExcelUtil.writeExcel(BusiAdvancedAlarmVo.class, "自定义告警表", title, list,headList,response);
		} catch (IOException e) {
			log.error("自定义告警列表下载异常!");
		}
	}

}
